package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.NeedDetailsEntity;
import com.sgb.kjwl.model.entity.enterprise.NeedDetailSignUpEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NeedSignUpListViewModel extends AndroidViewModel {


    public MutableLiveData<List<NeedDetailSignUpEntity>> listdata = new MutableLiveData<>();
    public MutableLiveData<String> ToastShow = new MutableLiveData<>();
    //1show,2dismiss
    public MutableLiveData<Integer> showloading = new MutableLiveData<>();
    public MutableLiveData<NeedDetailsEntity> mData = new MutableLiveData<>();
//    public NeedDetailsEntity mData;
    public String itemid = "";
    public boolean loadmore = false;
    public boolean ishome = false;
    private int page = 1;

    public NeedSignUpListViewModel(@NonNull @NotNull Application application) {
        super(application);
    }


    public void getListData(boolean loadmore){
        this.loadmore = loadmore;
        if(loadmore){
            page++;
        }else{
            page=1;
        }
        showloading.postValue(1);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getSignUpList(itemid+"",page+"",20).enqueue(new Callback<BaseEntity<ListResponse<List<NeedDetailSignUpEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<NeedDetailSignUpEntity>>>> call, Response<BaseEntity<ListResponse<List<NeedDetailSignUpEntity>>>> response) {
                showloading.postValue(2);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if(response.body().getData().getList()!=null){
                        listdata.postValue(response.body().getData().getList());
                    }
                } else {
                    ToastShow.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<NeedDetailSignUpEntity>>>> call, Throwable t) {
                listdata.postValue(new ArrayList<>());
                showloading.postValue(2);
                ToastShow.postValue(t.getMessage());
            }
        });


    }


    /**
     * 获取需求详情判定需求状态是否结束
     */
    public void getNeeddetail(){
        APIManager.getInstance().getNeedAPI().getDetails(itemid).enqueue(new Callback<BaseEntity<NeedDetailsEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<NeedDetailsEntity>> call, Response<BaseEntity<NeedDetailsEntity>> response) {

                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mData.postValue(response.body().getData());
                } else {
                    ToastShow.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<NeedDetailsEntity>> call, Throwable t) {
                ToastShow.postValue( t.getMessage());

            }
        });
    }

}
